﻿using System.Reflection;

namespace _73_C_反射调用其他程序集类定义的方法_包括静态方法_重载方法_实例方法_私有方法_
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.LoadFrom("TestClassLibrary.dll");
            //加载类型  程序集名称.类型命名
            Type type = assembly.GetType("TestClassLibrary.Student");
            //实例化对象
            Object obj = Activator.CreateInstance(type);
            //获取方法
            MethodInfo methodInf = type.GetMethod("Show");//无参方法
            //调用方法
            methodInf.Invoke(obj, new object[] { });

            MethodInfo methodInf1 = type.GetMethod("ShowInfo", new Type[] { typeof(string) });//有参方法
            methodInf1.Invoke(obj, new object[] { "张三" });

            MethodInfo methodInf2 = type.GetMethod("ShowInfo", new Type[] { typeof(string), typeof(int) });//有参重载方法
            methodInf2.Invoke(obj, new object[] { "", 0 });

            MethodInfo methodInf3 = type.GetMethod("GetName");//静态方法
            object obj1 = methodInf3.Invoke(null, new object[] { });//返回值
            Console.WriteLine("" + obj1);

            MethodInfo methodInf4 = type.GetMethod("GetAge", BindingFlags.Instance | BindingFlags.NonPublic);
            object obj2 = methodInf4.Invoke(obj,new object[] { });
            Console.WriteLine(obj2);
            Console.ReadKey();
        }
    }
}
